package cn.iocoder.yudao.module.pay.api.wallet;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pay.api.wallet.dto.PayWalletDTO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class PayWalletApiImpl implements PayWalletApi{

    @Resource
    private PayWalletMapper walletMapper;
    @Override
    public PayWalletDTO getOrCreateWallet(Long userId, Integer userType) {
        PayWalletDO wallet = walletMapper.selectByUserIdAndType(userId, userType);
        if (wallet == null) {
            wallet = new PayWalletDO().setUserId(userId).setUserType(userType)
                    .setBalance(0).setTotalExpense(0).setTotalRecharge(0).setCreditAmount(0).setAmountOwe(0).setPoint(0);
            wallet.setCreateTime(LocalDateTime.now());
            walletMapper.insert(wallet);
        }

        return BeanUtils.toBean(wallet,PayWalletDTO.class);
    }
}
